t = codesters.Teacher()
tm = TestManager()
stage.set_axis(0, 10)
""" ONLY CHANGE WHAT IS BETWEEN THE GREEN DASHES:
---------------------------------------------------------------"""
# This array represents the board.
# 1's are blocks and 0's are open pathways.
maze_data = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 0, 0, 1, 1],
[1, 1, 1, 1, 1, 0, 0, 1, 1, 1],
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
[1, 1, 1, 0, 0, 1, 1, 1, 1, 1],
[1, 1, 0, 0, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
]
# This is where the turtle starts.
start_position = (1, 10)
# This is where the goal is.
goal_position = (9, 2)
# This is the direction the turtle points to begin with
start_direc = 0
# This is the scale factor for the blocks.
block_size = 1
end_result = "incomplete"
# This function iterates through the maze_data array and draws the squares.
def draw_board():
board = maze_data
for j in range(len(board)):
for i in range (len(board[j])):
if board[j][i] == 1:
block_x = (i + 0.5) * block_size
block_y = (j + 0.5) * block_size
block = codesters.Square(block_x, block_y, block_size, "sienna", "black")
elif board[j][i] == 2:
block_x = (i + 0.5) * block_size
block_y = (j + 0.5) * block_size
block = codesters.Square(block_x, block_y, block_size, None, "black")
# trigger_block = codesters.Square(block_x, block_y, block_size, "green", "black")
trigger_block = codesters.Sprite("key", block_x, block_y)
elif board[j][i] == 0:
block_x = (i + 0.5) * block_size
block_y = (j + 0.5) * block_size
block = codesters.Square(block_x, block_y, block_size, None, "black")
def create_goal():
goal_x = (goal_position[0] - 0.5) * block_size
goal_y = (goal_position[1] - 0.5) * block_size
goal_block = codesters.Square(goal_x, goal_y, block_size, "lightyellow", "black")
return goal_block
# goal_block.cannot_collide()
draw_board()
goal_block = create_goal()
goal_x = (goal_position[0] - 0.5) * block_size
goal_y = (goal_position[1] - 0.5) * block_size
door = codesters.Square(goal_x, goal_y, block_size, "black", "black")
txt = codesters.Text("Guide the hedgehog all the way around the square to collect the keys.", 0, 225, "white")
sprite = codesters.Sprite("hedgehog")
sprite.set_size(0.3)
sprite.set_speed(2)
# This function puts the turtle at the starting position and rotation.
def send_sprite_home(t):
t.set_x((start_position[0] - 0.5 ) * block_size)
t.set_y((start_position[1] - .5) * block_size)
t.set_rotation(start_direc)
send_sprite_home(sprite)
trigger_count = 0
collision_state = 0
# This defines what happens when the turtle reaches the goal.
def collision(sprite, hit_sprite):
global collision_state
global goal_position
global block_size
color = hit_sprite.get_color()
if color == "black":
goal_x = (goal_position[0] - 0.5) * block_size
goal_y = (goal_position[1] - 0.5) * block_size
sprite.glide_to(goal_x, goal_y)
sprite.reset_animation()
stage.wait(.5)
hit_sprite.set_color("yellow")
# stage.remove_sprite(sprite)
stage.remove_sprite(txt)
if collision_state < 1:
tm.display_success_message("Great job!")
collision_state += 1
elif color == "sienna":
sprite.reset_animation()
hit_sprite.set_color("lightblue")
send_sprite_home(sprite)
stage.remove_sprite(txt)
if collision_state < 1:
tm.display_failure_message("You hit a block. Try again.")
collision_state += 1
# txt.set_text("You hit a block. Try Again")
sprite.event_collision(collision)
sprite.pen_down()
for counter in range(3):
sprite.move_forward(1)
sprite.turn_right(90)
sprite.move_forward(1)
sprite.turn_left(90)
sprite.pen_down()